package com.health.db.service.order.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.health.db.dao.order.ISubRegisterOrderDao;
import com.health.db.dao.user.IUserCardJournalDao;
import com.health.db.dao.user.IUserCardListDao;
import com.health.db.model.SubRegisterOrder;
import com.health.db.model.SubRegisterOrderExample;
import com.health.db.model.UserAccountJournal;
import com.health.db.model.UserCardJournal;
import com.health.db.model.UserCardList;
import com.health.db.model.UserCardListExt;
import com.health.db.service.order.ISubRegisterOrderService;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;

public class SubRegisterOrderServiceImpl implements ISubRegisterOrderService {

	private  ISubRegisterOrderDao subRegisterOrderDao;
	private IUserCardJournalDao userCardJournalDao;
	private IUserCardListDao userCardListDao;
	
	
	@Override
	public int insert(SubRegisterOrder record) {
		// TODO Auto-generated method stub
		return subRegisterOrderDao.insert(record);
	}

	@Override
	public SubRegisterOrder selectByPrimaryKey(Long registerid) {
		// TODO Auto-generated method stub
		return subRegisterOrderDao.selectByPrimaryKey(registerid);
	}

	@Override
	public List<SubRegisterOrder> selectSimple(SubRegisterOrderExample example) {
		// TODO Auto-generated method stub
		return subRegisterOrderDao.selectSimple(example);
	}

	@Override
	public int updateOrderSelective(SubRegisterOrder record) {
		// TODO Auto-generated method stub
		return subRegisterOrderDao.updateOrderSelective(record);
	}
	
	@Override
	public SubRegisterOrderExample updateOrder4Payment(SubRegisterOrder record,String outTradeNo) {
		// TODO Auto-generated method stub
		
		  int result=-1;
		  
		  SubRegisterOrderExample registerOrder =new SubRegisterOrderExample();
		  if (record.getSubOrderType().intValue()==4&&null!=record.getExpendCard()&&1==record.getExpendCard().intValue()) 
			{
				UserCardListExt queryUserCardListExt =new UserCardListExt();
				queryUserCardListExt.setOwnerid(record.getUserId());
				queryUserCardListExt.setLefttime04(new BigDecimal(1));
				List<UserCardList> list =userCardListDao.selectUserCardLists(queryUserCardListExt);
				
				if(null==list||list.size()==0 )
				{
					registerOrder.setUpdateNum(-2);
					return registerOrder;
				}
				
				UserCardList userCard =list.get(0);
				BigDecimal leftTime= userCard.getLefttime04();
				//更新 金额 和日期 ,卡号为条件
				userCard.setLefttime04(leftTime.subtract(new BigDecimal(1)));
				userCard.setUpdateDate(DateUtils.now());
				userCard.setCardid(userCard.getCardid());
				if(this.updateOrder4CardPayment(record, list.get(0))!=1){
					registerOrder.setUpdateNum(-2);
					return registerOrder;
				}
				record.setCardid(String.valueOf(userCard.getCardid()));
				record.setCarditemid("T"+Constant.PAYMENT_SERVICE_CARD_4);
			}
		  
		  result =subRegisterOrderDao.updateOrderSelective(record);
		
		  UserAccountJournal accountJournal = new UserAccountJournal();
			//支付成功
			if (record.getPaytypeid().shortValue()==Constant.CONSTANTS_PAYMENT_WEIXIN) {
				accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_2 +"-"+Constant.CONSTANTS_VOUCHERNAME_41+"订单号"+outTradeNo);
				accountJournal.setVoucherName(Constant.CONSTANTS_PAY_ACTION_ID_43+"-"+Constant.CONSTANTS_VOUCHERNAME_2 );
			}else if(record.getPaytypeid().shortValue()==Constant.CONSTANTS_PAYMENT_ALIPAY){
				accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_4 +"-"+Constant.CONSTANTS_VOUCHERNAME_41+"订单号"+outTradeNo);
				accountJournal.setVoucherName(Constant.CONSTANTS_PAY_ACTION_ID_43+"-"+Constant.CONSTANTS_VOUCHERNAME_4 );
			}
			accountJournal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_43);
			//商户订单号 作为备注
			accountJournal.setComment(outTradeNo);
			registerOrder.setUpdateNum(result);
		return registerOrder;
	}
	
	
	public int serviceCardPayment(long userId,SubRegisterOrder  currRegisterOrder, List<UserCardList>  userCardList,String type )
	{
		    /*long  orderId=currRegisterOrder.getRegisterid();
		    
		    //准备入库的数据对象
		    SubRegisterOrder orderInfo = new SubRegisterOrder();
		    //返回对象
		    RegisterOrderExample registerOrderExample = new RegisterOrderExample();
			orderInfo.setSubOrderId(orderId);
            Date currDateTime=DateUtils.getDatetime();
			orderInfo.setUpdateDate(currDateTime);
			
			UserCardList userCard =userCardList.get(0);
			
			if (Constant.PAYMENT_SERVICE_CARD_4.equals(type)) 
			{
				BigDecimal leftTime= userCard.getLefttime04();
				userCard.setLefttime04(leftTime.subtract(new BigDecimal(1)));
				
					orderInfo.("T"+String.valueOf(userCardList.get(0).getCardid()));
		
			}
			else if (Constant.PAYMENT_SERVICE_CARD_5.equals(type)) 
			{
				// OrderBillTime未写入registe_order表
				BigDecimal leftTime= userCard.getLefttime05();
				userCard.setLefttime05(leftTime.subtract(new BigDecimal(1)));
				if (StringUtils.isBlank(cardItemID)) {
					orderInfo.setCardItemID("T"+Constant.PAYMENT_SERVICE_CARD_5);
				}else{
					orderInfo.setCardItemID(cardItemID+","+"T"+Constant.PAYMENT_SERVICE_CARD_5);
				}
			}
			
			String cardJournalAmount = currRegisterOrder.getCardJournalAmount();
			String cardID =currRegisterOrder.getCardID();
			
			
			
			if (StringUtils.isBlank(cardJournalAmount)) {
				orderInfo.setCardJournalAmount("1");
			}else{
				orderInfo.setCardJournalAmount(cardJournalAmount+","+"1");
			}
			
			if (StringUtils.isBlank(cardID)) {
				orderInfo.setCardID(String.valueOf(userCardList.get(0).getCardid()));
			}else{
				orderInfo.setCardID(cardID+","+String.valueOf(userCardList.get(0).getCardid()));
			}
			
			
			//更新 金额 和日期 ,卡号为条件
			userCard.setUpdateDate(DateUtils.now());
			userCard.setCardid(userCard.getCardid());
			//registerOrderExample = registerOrderService.updateOrder4CardPaymentDoc(orderInfo, userCard,type);
			
			return  registerOrderExample.getUpdateNum();*/
		return -1;
	}
	/**
	 * record:订单信息
	 * userCardList：用户卡信息
	 * scheduleInfo：预约信息
	 * 
	 */
	public int updateOrder4CardPayment(SubRegisterOrder record, UserCardList userCardList ) {
	
		int result=-1;
	    //更新card账户数据
		result =userCardListDao.updateByPrimaryKeySelective(userCardList);
		
		if (result==1) {
 
	    	//处理更新卡支付日记账表
			UserCardJournal userCardJournal = new UserCardJournal();
			userCardJournal.setJournalAmount(new BigDecimal(-1));
	    	userCardJournal.setCardid(userCardList.getCardid());
	    	userCardJournal.setOwnerid(userCardList.getOwnerid());
	    	userCardJournal.setCarditemid((short)userCardList.getCardtypeid().intValue());
	    	userCardJournal.setJournalDate(new Date());
	    	if (record.getSubOrderType().intValue()==Short.valueOf(Constant.PAYMENT_SERVICE_CARD_4).intValue()) {
	    		userCardJournal.setJournalDesc("送药服务");
			}else if (record.getSubOrderType().intValue()==Short.valueOf(Constant.PAYMENT_SERVICE_CARD_5).intValue()){
				userCardJournal.setJournalDesc("挂号服务");
			}
	    	userCardJournal.setGoodsid(-1);
	    	userCardJournal.setVoucherid(record.getSubOrderId());
	    	userCardJournal.setComment(String.valueOf(record.getSubOrderType()));
	    	userCardJournal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_43);
	    	//记录日志
	    	userCardJournalDao.insert(userCardJournal);
		}
		return result;
	}
	
	
	@Override
	public int updateOrder4Cancel(SubRegisterOrder record) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public ISubRegisterOrderDao getSubRegisterOrderDao() {
		return subRegisterOrderDao;
	}

	public void setSubRegisterOrderDao(ISubRegisterOrderDao subRegisterOrderDao) {
		this.subRegisterOrderDao = subRegisterOrderDao;
	}

	public IUserCardJournalDao getUserCardJournalDao() {
		return userCardJournalDao;
	}

	public void setUserCardJournalDao(IUserCardJournalDao userCardJournalDao) {
		this.userCardJournalDao = userCardJournalDao;
	}

	public IUserCardListDao getUserCardListDao() {
		return userCardListDao;
	}

	public void setUserCardListDao(IUserCardListDao userCardListDao) {
		this.userCardListDao = userCardListDao;
	}
}
